<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;


class UserController extends Controller
{
    public function login(){//后台登录

        return view('admin.login');

    }
    public function dologin(Request $request){
        $name=$request->name;
        $password=$request->password;
        if(Auth::attempt(['name'=>$name,'password'=>$password])){
            return json_encode(['success'=>true,'msg'=>"登录成功"]);
        }else{
            return json_encode(['success'=>false,'msg'=>'登录失败！用户名或密码错误']);
        }
    }
    public function unsuccess(){
        return view('admin.loginunsuccess');
    }
    public function logout(){//用户注销
        Auth::logout();
        return redirect("/login");
    }
    public function adduser(){
        return view('admin.user-add-list');
    }
    public function store(Request $request){//新增用户请求
        $datas=$request->all();
        $rules=[
            'name'=>'required|alpha_dash|min:3|unique:users',
            'password'=>'required|min:6|confirmed'
        ];
        $messages=[
            'name.required'=>'用户名不能为空',
            'name.alpha_dash'=>'用户名应该是数字、字母或者下划线组成',
            'name.min'=>'用户名的长度不能少于:min个字符',
            'name.unique'=>'用户名已经存在，请重新输入',
            'password.required'=>'密码不能为空，请输入密码',
            'password.min'=>'密码的长度不能少于:min个字符',
            'password.confirmed'=>'两次密码不一致，请重新输入',
        ];
        $validator=Validator::make($datas,$rules,$messages);//验证
        if($validator->fails()){//验证失败
            $msg=$validator->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        else{
            $datas['password']=Hash::make($datas['password']);//哈希加密
            User::create($datas);//创建新的用户记录
            return json_encode(['success'=>true,'msg'=>'success']);
        }

    }
    public function addusersuccess(){
        return view('admin.useraddsuccess');
    }
    public function edituser($id){//修改用户密码
        $user=User::find($id);
        if($user==null){
            abort(404);
        }
        return view('admin.edit-user')->with('user',$user);
    }
    public function update($id,Request $request){
        $user=User::find($id);
        if($user==null){
            abort(404);
        }
        $datas=$request->all();
        $rules=[
            'password'=>'required|min:6|confirmed'
        ];
        $messages=[
            'password.required'=>'密码不能为空，请输入密码',
            'password.min'=>'密码长度不能少于:min个字符',
            'password.confirmed'=>'两次密码输入不一致，请重新输入'
        ];
        $validator=Validator::make($datas,$rules,$messages);
        if($validator->fails()){
            $msg=$validator->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        else{
            $user->password=Hash::make($datas['password']);
            $user->save();
            return json_encode(['success'=>true,'msg'=>'success']);
        }
    }
    public function editusersuccess(){
        return view('admin.usereditsuccess');
    }
    public function deluser($id){//删除用户
        $user=User::find($id);
        $user->delete();
        return view('admin.userdeletesuccess');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('admin.admin-index');

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
   // public function store(Request $request)
   // {
     //   //
    //}

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //


    }
    public function userlist(){
        $users = User::paginate(9);
        return view('admin.user-show-list')->with('users',$users);
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //public function update(Request $request, $id)
   // {
        //
   // }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
